package com.mtoolkit.jdbc.support;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.Future;

import javax.sql.DataSource;

import com.mtoolkit.jdbc.JdbcTemplate;
import com.mtoolkit.jdbc.PersistenceException;
import com.mtoolkit.jdbc.handler.ResultSetHandler;
import com.mtoolkit.page.Page;

/**
 * Decorate JdbcTemplate class.
 *
 * @author 	<a href="mailto:shixi.zhang@f-club.com">ZhangShixi</a>
 * @version 1.00, 5/22/2012
 * @since 	JDK1.5
 */
public class DecorateJdbcTemplate {
	
	private DataSource   _dataSource;
	private JdbcTemplate _jdbcTemplate;
	
	public DecorateJdbcTemplate(
		   DataSource dataSource, JdbcTemplate jdbcTemplate) {
		_dataSource = dataSource;
		_jdbcTemplate = jdbcTemplate;
	}
	
	public Number executeKeyInsert(String keySql, String insertSql) {
		
		return _jdbcTemplate.executeKeyInsert(
				getConnection(), keySql, insertSql);
	}
	
	public Number executeKeyInsert(String keySql, 
								   String insertSql, 
								   Object... params) {
		
		return _jdbcTemplate.executeKeyInsert(
				getConnection(), keySql, insertSql, params);
	}

	public Number[] executeKeyInsert(String keySql, 
									 String insertSql, 
									 Object[][] params) {
		
		return _jdbcTemplate.executeKeyInsert(
				getConnection(), keySql, insertSql, params);
	}

	public Number[] executeKeyInsert(String keySql, 
									 String insertSql, 
									 Object[][] params, 
									 int batchSize) {
		
		return _jdbcTemplate.executeKeyInsert(
				getConnection(), keySql, insertSql, params, batchSize);
	}

	public int executeUpdate(String sql) {
		return _jdbcTemplate.executeUpdate(
				getConnection(), sql);
	}

	public int executeUpdate(String sql, Object... params) {
		
		return _jdbcTemplate.executeUpdate(
				getConnection(), sql, params);
	}

	public int[] executeBatchUpdate(String sql, 
									Object[][] params) {
		
		return _jdbcTemplate.executeBatchUpdate(
				getConnection(), sql, params);
	}

	public int[] executeBatchUpdate(String sql, 
									Object[][] params, 
									int batchSize) {
		
		return _jdbcTemplate.executeBatchUpdate(
				getConnection(), sql, params, batchSize);
	}

	public Future<Integer> executeDelayUpdate(String sql) {
		
		return _jdbcTemplate.executeDelayUpdate(
				getConnection(), sql);
	}

	public Future<Integer> executeDelayUpdate(String sql, 
									 		  Object... params) {
		
		return _jdbcTemplate.executeDelayUpdate(
				getConnection(), sql, params);
	}

	public Future<Integer[]> executeBatchDelayUpdate(String sql, 
													 Object[][] params) {
		
		return _jdbcTemplate.executeBatchDelayUpdate(
				getConnection(), sql, params);
	}

	public Future<Integer[]> executeBatchDelayUpdate(String sql, 
													 Object[][] params, 
													 int batchSize) {
		
		return _jdbcTemplate.executeBatchDelayUpdate(
				getConnection(), sql, params, batchSize);
	}

	public <T> T executeQuery(String sql, 
							  ResultSetHandler<T> handler) {
		
		return _jdbcTemplate.executeQuery(
				getConnection(), sql, handler);
	}

	public <T> T executeQuery(String sql, 
							  ResultSetHandler<T> handler, 
							  Object... params) {
		
		return _jdbcTemplate.executeQuery(
				getConnection(), sql, handler, params);
	}

	public long executeCountQuery(String sql) {
		return _jdbcTemplate.executeCountQuery(getConnection(), sql);
	}

	public long executeCountQuery(String sql, Object... params) {
		
		return _jdbcTemplate.executeCountQuery(
				getConnection(), sql, params);
	}

	public <T> T executePageQuery(String countSql, 
								  String querySql, 
								  Page<?> page, 
								  ResultSetHandler<T> handler) {
		
		return _jdbcTemplate.executePageQuery(
				getConnection(), countSql, querySql, page, handler);
	}

	public <T> T executePageQuery(String countSql,
								  String querySql, 
								  Page<?> page, 
								  ResultSetHandler<T> handler,
								  Object... params) {
		
		return _jdbcTemplate.executePageQuery(
				getConnection(), countSql, 
				querySql, page, handler, params);
	}

	public <T> T executePageQuery(String countSql,
								  String querySql, 
								  Page<?> page, 
								  ResultSetHandler<T> handler,
								  int limitSize, 
								  Object... params) {
		
		return _jdbcTemplate.executePageQuery(
				getConnection(), countSql, querySql,
				page, handler, limitSize, params);
	}
	
	// ---- private methods --------------------------------
	private Connection getConnection() {
		try {
			return _dataSource.getConnection();
		} catch(SQLException e) {
			throw new PersistenceException(
				  "Cound not get connection from datasource.", e);
		}
	}

}
